package com.hellodoctor.appointment;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletSession;

import com.hellodoctor.slayer.model.Profile;
import com.hellodoctor.slayer.model.impl.AppointmentImpl;
import com.hellodoctor.slayer.service.AppointmentLocalServiceUtil;
import com.hellodoctor.slayer.service.ProfileLocalServiceUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class Appointment
 */
public class Appointment extends MVCPortlet {

@Override
public void init() throws PortletException {
	super.init();
	addProcessActionSuccessMessage = false;
}

public void changeStatus(ActionRequest actionRequest,
		ActionResponse actionResponse) throws IOException, PortletException {
	int status = ParamUtil.getInteger(actionRequest, "status");
	long appointmentId =  ParamUtil.getLong(actionRequest, "appointmentId");
	boolean returnToMyPage = ParamUtil.getBoolean(actionRequest, "isMyPage");
	System.out.println("Flag-"+returnToMyPage);
	com.hellodoctor.slayer.model.Appointment appointment = new AppointmentImpl();
		try {
			appointment=AppointmentLocalServiceUtil.fetchAppointment(appointmentId);
		} catch (SystemException e) {
			e.printStackTrace();
		}
		appointment.setStatus(status);
		appointment.setModifiedDate(new Date());
		try {
			appointment = AppointmentLocalServiceUtil.updateAppointment(appointment);
		} catch (SystemException e) {
			e.printStackTrace();
		}
		System.out.println("Appointment status update");
	if(returnToMyPage)
		actionResponse.setRenderParameter("jspPage","/html/appointment/allAppointments.jsp");
	
}

public void searchDoctor(ActionRequest actionRequest,
		ActionResponse actionResponse) throws IOException, PortletException {
	System.out.println("Inside appointment java"+ParamUtil.getInteger(actionRequest, "byExperience"));
	int experience, speciality, qualification = 0;
	experience =  ParamUtil.getInteger(actionRequest, "byExperience");
	speciality =  ParamUtil.getInteger(actionRequest, "bySpeciality");
	qualification= ParamUtil.getInteger(actionRequest, "byQualification");
	experience = ParamUtil.getInteger(actionRequest, "byExperience");
	 System.out.println("Experience-"+experience);
	
	List<Profile> doctors = null;
	try {
	 doctors = ProfileLocalServiceUtil.searchDoctors(speciality,qualification,experience);
	 System.out.println("Doctors-"+doctors);
	} catch (SystemException e) {
		e.printStackTrace();
	}
	actionRequest.setAttribute("doctors", doctors);
}

	public void makeAppointment(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
	
        System.out.println("Inside Appointment action class");
		long doctorId=ParamUtil.getLong(actionRequest, "doctorId");
		long patientId=ParamUtil.getLong(actionRequest, "patientId");
		int appointmentTime = ParamUtil.getInteger(actionRequest, "appointmentTime");
		String appointmentDate = ParamUtil.getString(actionRequest, "test");
		
		System.out.println("doctorId-"+doctorId+"  patientId-"+patientId );
		com.hellodoctor.slayer.model.Appointment appointment = AppointmentLocalServiceUtil.createAppointment(doctorId, patientId, 
																appointmentDate, appointmentTime);

		PortletSession portSession = actionRequest.getPortletSession();
		portSession.setAttribute("appointmentId",String.valueOf(appointment.getAppointmentId()));
		actionResponse.sendRedirect(ParamUtil.getString(actionRequest, "redirectUrl"));
	}
	
	
}
